package com.lovo.pis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.pis.dto.PageBean;
import com.lovo.pis.dto.SellCarGoodsBean;
import com.lovo.pis.dto.StockGoodsBean;
import com.lovo.pis.mapper.SellCarGoodsMapper;
import com.lovo.pis.mapper.StockGoodsMapper;
import com.lovo.pis.service.ISellCarGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SellCarGoodsServiceImpl extends ServiceImpl<SellCarGoodsMapper, SellCarGoodsBean> implements ISellCarGoodsService {

    @Autowired
    SellCarGoodsMapper sellCarGoodsMapper;
    @Autowired
    StockGoodsMapper stockGoodsMapper;

    @Override
    public IPage<SellCarGoodsBean> queryAllBuyCarGoods(PageBean<SellCarGoodsBean> pageInfo) {
        return sellCarGoodsMapper.selectAllBuyCarGoods(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()),pageInfo.getData().getCarId());
    }

    @Override
    public double getSumPrice(int carId) {
        List<SellCarGoodsBean> sellList = sellCarGoodsMapper.selectAllSellCarGoods(carId);
        double sumPrice = 0;
        for (int i = 0; i < sellList.size(); i++) {
           sumPrice+=sellList.get(i).getPrice()*sellList.get(i).getNumber();
        }
        return sumPrice;
    }

    @Override
    public int changeItem(int carId, int id, int number) {
        StockGoodsBean stockGoodsBean = stockGoodsMapper.selectById(id);
        if (stockGoodsBean.getStockNumber() >= number) {
            sellCarGoodsMapper.updateNumber(carId, id, number);
            return 0;
        }else {
            return stockGoodsBean.getStockNumber();
        }
    }

    @Override
    public void removeById(int carId, int id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("car_id", carId);
        queryWrapper.eq("id", id);
        sellCarGoodsMapper.delete(queryWrapper);
    }

    @Override
    public void addBuyCarGoods(int id,int carId) {
        StockGoodsBean stockGoodsBean = stockGoodsMapper.selectById(id);
        SellCarGoodsBean sellCarGoodsBean = new SellCarGoodsBean(stockGoodsBean.getId(),stockGoodsBean.getName(),stockGoodsBean.getBrand(),stockGoodsBean.getType(),stockGoodsBean.getSellPrice(),0,carId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", sellCarGoodsBean.getId());
        queryWrapper.eq("car_id", sellCarGoodsBean.getCarId());
        SellCarGoodsBean goodsBean = sellCarGoodsMapper.selectOne(queryWrapper);
        if (goodsBean != null) {
            goodsBean.setNumber(goodsBean.getNumber() + sellCarGoodsBean.getNumber());
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("car_id", sellCarGoodsBean.getCarId());
            updateWrapper.eq("id", sellCarGoodsBean.getId());
            sellCarGoodsMapper.update(goodsBean, updateWrapper);
        } else {
            sellCarGoodsMapper.insert(sellCarGoodsBean);
        }
    }

}
